package com.daocao.hua.api.requestInfo;

import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.OpinionBean;
import com.daocao.hua.api.bean.UploadImageBean;
import com.daocao.hua.api.callback.OpinionContract;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class OpinionRequest extends BaseRequest<OpinionContract.View> implements OpinionContract.Presenter<OpinionContract.View> {

    public static OpinionRequest create() {
        return new OpinionRequest();
    }


    @Override
    public void uploadImg(int poi,String img) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        UploadImageBean bean = new UploadImageBean();
        bean.img = img;
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.uploadImgSuccess(poi,result);
                });
    }

    @Override
    public void getOpinionInfo(String type, String content, String photo) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        OpinionBean bean = new OpinionBean();
        bean.content = content;
        bean.photo = photo;
        bean.type = type;
        bean.from = "Android";
        Api.api_service.getOpinionInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                        mView.showError();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getOpinionInfoSuccess(result);
                });
    }

    @Override
    public void getOpinionTypeInfo() {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        Api.api_service.getOpinionTypeInfo().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                        mView.showError();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getOpinionTypeInfoSuccess(result);
                });
    }
}
